home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / Target Acquisition Manager / Source / source / FinderFun.cp < prev    next >
Encoding:
Text File  |  1999-06-25  |  3.9 KB  |  149 lines  |  [TEXT/CWIE]

  1. #include "FinderFun.h"
  2.  
  3. #define    kFinderType            'FNDR'
  4. #define    kFinderSignature    'MACS'
  5. #define kWhoCares            '????'
  6.  
  7.  
  8. static void FinderFile(AEEventID finderAction);
  9.  
  10. //    Used as an object specifier for the current selection
  11. static long gSelectionAEData[] = 
  12. {
  13.     4, 0    ,'form','enum',
  14.     4,'prop','want','type',
  15.     4,'prop','seld','type',
  16.     4,'sele','from','null',
  17.     0
  18. };
  19.  
  20.  
  21. /*    leonardr [022599]
  22.     A routine to determine the process serial number of the Finder. It returns the result as a
  23.     parameter passed by reference. It also has a Boolean parameter which specifies whether or not
  24.     the returned process serial number will represent the Finder process serial number as
  25.     "kCurrentProcess" if it is the current process to allow a shortcut of the _GetNextEvent call
  26.     dependencies of processing an AppleEvent.
  27.  
  28.      Input:    shortcut - allow "kCurrentProcess" to be used if we are in the current process.
  29.      Input:    *finderpsn - result ProcessSerialNumber passed by reference.
  30.  
  31.     Output:    error code that occured. 
  32. */
  33.  
  34.  
  35. OSErr    GetFinderProcess (ProcessSerialNumber *finderpsn, Boolean shortcut)
  36. {
  37.     Boolean                result;
  38.     ProcessSerialNumber    psn, currentpsn;
  39.     ProcessInfoRec        pir;
  40.  
  41.     psn.highLongOfPSN = 0;
  42.     psn.lowLongOfPSN = kNoProcess;
  43.     pir.processInfoLength = sizeof(ProcessInfoRec);
  44.     pir.processName = nil;            // don't want these bits of information
  45.     pir.processAppSpec = nil;
  46.     
  47.     while (GetNextProcess(&psn) == noErr)
  48.     {
  49.         if (GetProcessInformation(&psn, &pir) == noErr)
  50.         {
  51.             if ((pir.processType == kFinderType) && (pir.processSignature == kFinderSignature)) 
  52.             {
  53.                 if (shortcut && (GetCurrentProcess(¤tpsn) == noErr) && 
  54.                     (SameProcess(¤tpsn, &psn, &result) == noErr) && result) 
  55.                 {            // use the current process to shortcut the event dispatching
  56.                     finderpsn->highLongOfPSN = 0;
  57.                     finderpsn->lowLongOfPSN = kCurrentProcess;
  58.                 }
  59.                 else
  60.                 {
  61.                     *finderpsn = psn;            // found the Finder's psn
  62.                 }
  63.                 return(noErr);            // got the process serial number
  64.             }
  65.         }
  66.     }
  67.     
  68.     return(procNotFound);            // got an error - not found
  69. }
  70.  
  71. //•••••••• Move File ••••••••
  72. void MoveFileToTrash (void)
  73. {
  74.     FinderFile(kAEMove);
  75. }
  76.  
  77. //•••••••• Finder File ••••••••
  78. static void FinderFile(AEEventID finderAction)
  79. {
  80.     AEDesc        AEFinderAddress;
  81.     AppleEvent    theEvent;
  82.     ProcessSerialNumber    finderPSN;
  83.     
  84.     GetFinderProcess (&finderPSN, true);
  85.     if (!AECreateDesc(typeProcessSerialNumber, (Ptr)&finderPSN, sizeof(ProcessSerialNumber), &AEFinderAddress))
  86.     {
  87.         if (!AECreateAppleEvent(kAECoreSuite, finderAction, &AEFinderAddress, kAutoGenerateReturnID,
  88.             kAnyTransactionID, &theEvent))
  89.         {        
  90.             //    Source
  91.             FSSpec                    DocSpec;
  92.             short                     vRefNum;
  93.             long                     dirID;
  94.             OSErr                    error;
  95.             
  96.             //    No source files specified, use current selection
  97.             AEPutParamPtr(&theEvent, (finderAction == kAECreateElement) ? 'to  ' : keyDirectObject,
  98.                           cObjectSpecifier,(Ptr)&gSelectionAEData,sizeof(gSelectionAEData));
  99.             
  100.             //    Destination
  101.             
  102.             
  103.             error = FindFolder(-1, kTrashFolderType, true, &vRefNum, &dirID);
  104.             
  105.             error = FSMakeFSSpec(vRefNum, dirID, "\p", &DocSpec);
  106.             
  107.             if (error == noErr)
  108.             {
  109.                 AliasHandle    docAlias;
  110.                 
  111.                 if (!NewAlias(0,&DocSpec,&docAlias))
  112.                 {
  113.                     HLock((Handle)docAlias);
  114.                     
  115.                     AEPutParamPtr(&theEvent,keyAEInsertHere,typeAlias,(Ptr)*docAlias,GetHandleSize((Handle)docAlias));
  116.                     
  117.                     DisposeHandle((Handle)docAlias);
  118.                 }
  119.             }
  120.             
  121.             //    Specify to replace existing files
  122.             // AEBuildParameters(&theEvent, "alrp:exsi");
  123.             AEPutParamPtr(&theEvent,'alrp','true',0,0);    // leonardr [061699]: try a valid value ;)
  124.             
  125.             //    Send the message
  126.             error = AESend(&theEvent, nil, kAENoReply, kAENormalPriority, 60, nil, nil);
  127. //            SendAppleEvent(&theEvent, blockScript);            
  128.         }
  129.         
  130.         AEDisposeDesc(&AEFinderAddress);
  131.     }
  132. }
  133.  
  134. //    730, 340
  135. void ClickOnIcon(Point inPoint)
  136. {
  137.     OSErr error;
  138.     
  139.     LMSetMouseLocation(inPoint);
  140.     
  141.     error = PostEvent(mouseDown, 0);
  142.     ThrowIfOSErr_(error);
  143.     
  144.     LMSetMouseLocation(inPoint);
  145.  
  146.     error = PostEvent(mouseUp, 0);
  147.     ThrowIfOSErr_(error);
  148. }
  149.